package com.powernode.lambda.stream;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 测试stream相关Api groupingBy分组收集
 *
 */
public class Stream10 {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of(
                "张无忌","韦一笑","赵敏","东方不败","西门吹雪","岳不群"
        );
        /**
         * Collectors.groupingBy方法第一个参数相当于key，另一个相当于value
         * 第一个参数function类型，返回分组规则
         * 第二个参数Collector类型，需要传入收集后的容器类型
         * 下面实现以姓名长度为key，收集相同姓名长度为value的一个hashmap
         */
//        Map<Integer, List<String>> collect = stream.collect(Collectors.groupingBy((x -> x.length()), Collectors.toList()));
//        for (Map.Entry<Integer, List<String>> entry : collect.entrySet()) {
//            System.out.println(entry);
//        }
        Map<Integer, String> collect = stream.collect(Collectors.groupingBy((x -> x.length()), Collectors.joining(",")));
        for (Map.Entry<Integer, String> entry : collect.entrySet()) {
            System.out.println(entry);
        }

    }

}
